﻿using System;
using System.Collections.Generic;

namespace Project2
{
    public interface ISubject
    {
        void AddObserver(IObserver observer);//添加观察者

        void RemoveObserver(IObserver observer);//移除观察者

        void NotifyObservers();//通知观察者
    }

    public interface IObserver
    {
        void Observe(string info);//通知之后，观察者执行动作
    }

    public class Observer : IObserver
    {
        public void Observe(string info)
        { }
    }

    public class CurrentConditionDisplay : IObserver
    {
        public void Observe(string message)
        {
            Console.WriteLine($"Current Condition Display: {message}");
        }
    }

    public class StatisticsDisplay : IObserver
    {
        public void Observe(string message)
        {
            Console.WriteLine($"Statistics Display: {message}");
        }
    }

    public class Subject : ISubject
    {
        private List<IObserver> _observers = new List<IObserver>();
        private string _weatherInfo;

        public void AddObserver(IObserver observer)
        {
            _observers.Add(observer);
        }

        public void RemoveObserver(IObserver observer)
        {
            _observers.Remove(observer);
        }

        public void NotifyObservers()
        {
            foreach (var observer in _observers)
            {
                observer.Observe(_weatherInfo);
            }
        }

        public void SetWeatherInfo(string weatherInfo)
        {
            _weatherInfo = weatherInfo;
            NotifyObservers();
        }
    }

    public class ProgramDesign1
    {
        private static void MainDesign1()
        {
            Subject subject = new Subject();

            IObserver currentConditionDisplay = new CurrentConditionDisplay();
            IObserver statisticsDisplay = new StatisticsDisplay();
            //添加观察者
            subject.AddObserver(currentConditionDisplay);
            subject.AddObserver(statisticsDisplay);
            //做出动作

            subject.SetWeatherInfo("Sunny and 25°C");
            subject.SetWeatherInfo("Rainy and 18°C");

            subject.RemoveObserver(statisticsDisplay);
            subject.SetWeatherInfo("Cloudy and 22°C");

            Console.WriteLine("asdfj");
            IList<int> count = new List<int>();
            count.Add(1);
            count.Add(2);
            count.Add(3);
            for (int i = 0; i < count.Count; i++)
            {
                Console.WriteLine(count[i]);
            }
        }
    }
}